<?php

namespace app\common\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class GoodsSku extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'goods_sku';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';


    // 追加属性
    protected $append = [
    ];

    public function getThumbnailImageAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['thumbnailimage']) ? $data['thumbnailimage'] : '');
        return get_app_image_url($value);
    }

    public function getSkuThumbnailAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sku_thumbnail']) ? $data['sku_thumbnail'] : '');
        return get_app_image_url($value);
    }
    public function getThumbnailImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['thumbnailimages']) ? $data['thumbnailimages'] : '');
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
//    public function admin()
//    {
//        return $this->belongsTo('Admin', 'check_id', 'id', [], 'LEFT')->setEagerlyType(0);
//    }


    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 通过规格获取商品信息
     * @author
     */
    public function getGoodsList($spec_ids,$type=0){
        if($type==2){
            $list = self::getGoods1List($spec_ids,$type);//兑换商品
        }else if($type==0){
            $list = self::getGoods0List($spec_ids,$type);//爆品
        }
        return $list;
    }

    /**
     * 爆品
     * @author
     */
    public function getGoods0List($spec_ids,$type=0){

        $list=static::alias('a')
            ->join('goods b', 'a.gid=b.id', 'LEFT')
            ->field('a.*,b.name  as goods_name,b.thumbnailimage,b.shop_id,b.is_ship,b.freight_type,b.freight_id,b.starttime,b.endtime,b.type,b.earnest,b.type1time')
            ->whereIn('a.id',$spec_ids)
            ->where('a.type',$type)
            ->select();


        return $list;
    }

    /**
     * 兑换商品
     * @author
     */
    public function getGoods1List($spec_ids,$type=0){

        $list=static::alias('a')
            ->join('goods1 b', 'a.gid=b.id', 'LEFT')
            ->field('a.*,b.name  as goods_name,b.thumbnailimage,b.shop_id,b.is_ship,b.freight_type,b.freight_id')
            ->whereIn('a.id',$spec_ids)
            ->where('a.type',$type)
            ->select();
        return $list;
    }

    /**
     * 商品规格
     * */
    public static function specList($goods_id,$type=0){
        $specList = Db::name('goods_spec')
            ->where('gid',$goods_id)
            ->where('type',$type)
            ->where('deletetime',null)
            ->order('sort')
            ->select();

        if(!empty($specList)){
            foreach ($specList as $k => $v){
                $specValueList = Db::name('goods_spec_value')
                    ->where('deletetime',null)
                    ->where('gid',$goods_id)
                    ->where('sid',$v['id'])
                    ->order('sort')
                    ->select();
                $specList[$k]['values'] = $specValueList;
            }
        }
        return $specList;
    }
    /**
     * 根据规格获取商品信息
     * */
    public static function getGoodsInfo($spec_ids,$type){
        $where['a.id']   = $spec_ids;
        $where['a.type'] = $type;
//        $where['b.state'] = 1;
//        $where['b.status'] = 1;
        $list = self::alias('a')
            ->join('goods b', 'a.gid=b.id', 'LEFT')
            ->join('shop_apply c', 'b.shop_id=c.id', 'LEFT')

            ->field('a.*,b.name  as goods_name,b.thumbnailimage,b.shop_id,b.is_ship,b.freight_type,b.freight_id,c.company_name,logo,city1')
            ->where($where)
            ->find();
        $list['logo'] = get_app_image_url($list['logo']);
        return $list;
    }
    /**
     * 根据规格获取商品信息
     * */
    public static function getGoods1Info($spec_ids,$type){
        $where['a.id']   = $spec_ids;
        $where['a.type'] = $type;
//        $where['b.state'] = 1;
//        $where['b.status'] = 1;
        $list = self::alias('a')
            ->join('goods1 b', 'a.gid=b.id', 'LEFT')
            ->join('shop_apply c', 'b.shop_id=c.id', 'LEFT')
            ->field('a.*,b.name  as goods_name,b.thumbnailimage,b.shop_id,b.is_ship,b.freight_type,b.freight_id,c.company_name,logo,city1')
            ->where($where)
            ->find();
        if(!empty($list)){

            $list['logo'] = get_app_image_url($list['logo']);
        }
        return $list;

    }
}
